package com.atguigu.sms.handler;

import com.atguigu.sms.utils.JacksonUtils;
import com.atguigu.sms.utils.PlaceHolderUtils;
import com.atguigu.sms.constant.SmsConstant;
import com.atguigu.sms.model.param.SmsPlaceHolderParameter;
import com.atguigu.sms.strategy.SmsSendRejectStrategy;
import com.atguigu.sms.strategy.SmsTemplateContext;

public class DefaultSmsTemplatePlaceHolderHandler implements SmsTemplatePlaceHolderHandler {

    private SmsSendRejectStrategy rejectStrateg;

    public DefaultSmsTemplatePlaceHolderHandler(SmsSendRejectStrategy rejectStrateg) {
        this.rejectStrateg = rejectStrateg;
    }

    @Override
    public String handler(SmsTemplateContext templateContext, SmsPlaceHolderParameter parameter) {
        rejectStrateg.reject(templateContext, parameter);
        return PlaceHolderUtils.replacePlaceHolder(
                templateContext.getTemplateContent(),
                JacksonUtils.toMap(parameter),
                SmsConstant.DEFAULT_PLACE_HOLDER_REGEX,
                SmsConstant.DEFAULT_PLACE_HOLDER_KEY_REGEX);
    }
}
